﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Data.SqlClient;

namespace WFA_Equals
{
    public partial class Form1 : Form
    {
        string fName = "";
        string fPath = "";
        string msg = "";

        string sKey = "";
        string sValue = "";
        string info = "";
        string[] fieldName;
        string SqlCmd = "";
        string _productid = "";


        MyLibary.ConfigLibary cfgLib = new MyLibary.ConfigLibary();
        MyLibary.DataBaseLibary dbLib;
        DataSet ds;
        MyLibary.ExcelLibary excelLib = new MyLibary.ExcelLibary();
        MyLibary.FileLibary fileLib = new MyLibary.FileLibary();
        MyLibary.XmlDocumentable xmldocable = new MyLibary.XmlDocumentable();
        MyLibary.XmlStreamRWable xmlstreamrwable = new MyLibary.XmlStreamRWable();

        public Form1()
        {
            InitializeComponent();            

            cobServiceDescription.SelectedIndex = 0;

            #region 给xx控件稍许的提示

            //ToolTipIcon:取得或设定值,以便定义要显示在工具提示文字旁的图示类型.
            ttTip.ToolTipIcon = ToolTipIcon.Info;
            //ForeColor:取得或设定工具提示的前景色彩.
            ttTip.ForeColor = Color.Blue;
            //BackColor:取得或设定工具提示的背景色彩.
            ttTip.BackColor = Color.LightGray;
            //AutoPopDelay:当指标静止於控制项上时,ToolTip 保持可见的时间 (以毫秒为单位).预设值为 5000.
            ttTip.AutoPopDelay = 5000;
            //ToolTipTitle:取得或设定工具提示视窗的标题.
            ttTip.ToolTipTitle = "^ v ^";

            #endregion  
        }

        #region ---------------------------------------------- Page1 Replace ----------------------------------------------

        //--------------------Event--------------------------
        private void btnConnecting_Click(object sender, EventArgs e)
        {
            InitReplace(cobServiceDescription.Text.Trim());
        }
        private void btnImporting_Click(object sender, EventArgs e)
        {
            string[] s1;
            string productid = "";

            if (cobServiceDescription.Text != "" && cobReplaceFields.Text != "" &&
                cobReplaceFields.Text != "" && txtSourceContent.Text != "" &&
                cobWorkProcess.Text != "")
            {
                if (Fun_FileDialog(ref fName, ref msg) == true)
                {
                    //打开excel
                    //读取excel的sheet内容
                    //关闭excel
                    ds = excelLib.ExcelToDS(fName);

                    s1 = new string[ds.Tables[0].Rows.Count];

                    //合成Sql指令
                    for (int i = 0; i <= ds.Tables[0].Rows.Count - 1; i++)
                    {
                        s1[i] = ds.Tables[0].Rows[i].ItemArray[0].ToString();

                        if (i < ds.Tables[0].Rows.Count - 1)
                        {
                            productid += "'" + s1[i] + "',";
                        }
                        else if (i == ds.Tables[0].Rows.Count - 1)
                        {
                            productid += "'" + s1[i] + "'";
                        }
                    }

                    _productid = productid;

                    txtReplaceContent.Text = _productid;
                }
            }
            else
            {
                MessageBox.Show("这些字段的内容不能为空！" + Environment.NewLine +
                                groupBox1.Text + Environment.NewLine +
                                label1.Text + Environment.NewLine +
                                label2.Text + Environment.NewLine +
                                label3.Text + Environment.NewLine +
                                label4.Text + Environment.NewLine, "错误",
                                MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        private void btnReplacing_Click(object sender, EventArgs e)
        {
            if (cobServiceDescription.Text != "" && cobReplaceFields.Text != "" &&
                cobReplaceFields.Text != "" && txtSourceContent.Text != "" &&
                cobWorkProcess.Text != "")
            {
                //显示Sqlcmd指令内容
                SqlCmd = "UPDATE " + getTab(cobServiceDescription.Text) + " " + "\r" +
                         "SET " + cobReplaceFields.Text + " = REPLACE(" + cobReplaceFields.Text +
                         ",'" + txtSourceContent.Text + "','" + txtRelacedContent.Text + "') " + "\r" +
                         "WHERE process ='" + cobWorkProcess.Text + "' AND productid IN(" + _productid + ")";
                Fun_DisplayMessage_Qualified("------------------------------------------");
                Fun_DisplayMessage_Qualified(SqlCmd);

                //执行Sqlcmd指令内容
                int RecordSetCount = 0;

                dbLib.sqllib.Query(SqlCmd, ref RecordSetCount);

                if (RecordSetCount != 0)
                {
                    Fun_DisplayMessage_Qualified("替换成功！返回条数：" + RecordSetCount);
                }
                else
                {
                    Fun_DisplayMessage_inQualified("替换失败！");
                }
            }
            else
            {
                MessageBox.Show("这些字段的内容不能为空！" + Environment.NewLine +
                                groupBox1.Text + Environment.NewLine +
                                label1.Text + Environment.NewLine +
                                label2.Text + Environment.NewLine +
                                label3.Text + Environment.NewLine +
                                label4.Text + Environment.NewLine, "错误",
                                MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        //--------------------Math--------------------------
        private string getTab(string ServiceDescription)
        {           
            string _section = "appSettings";
            string _name="";

            switch(ServiceDescription)
            {
                case"大批量车间目标数据":
                    _name="BigShop_Table";
                    break;

                case"大批量车间原始数据":
                    _name="BigShop_SourceDatabase_Table";
                    break;

                case"小批量车间":
                    _name="SmallShop_Table";
                    break;
            }
            
            if (cfgLib.GetConfigContent(_section, _name, ref sKey, ref sValue, ref info) == true)
            {
                return sValue;
            }
            else
            {
                return info;
            }            
        }
        private void InitReplace(string ServiceDescription)
        {
            string _sd = "";
            string _tab = "";

            switch (ServiceDescription)
            {
                case "大批量车间目标数据":

                    _sd = "BigShop_SqlConnectionString";
                    _tab = "BigShop_Table";

                    break;

                case "大批量车间原始数据":

                    _sd = "BigShop_SourceDatabase_SqlConnectionString";
                    _tab = "BigShop_SourceDatabase_Table";

                    break;

                case "小批量车间":

                    _sd = "SmallShop_SqlConnectionString";
                    _tab = "SmallShop_Table";

                    break;
            }

            if (cfgLib.GetConfigContent("appSettings", _sd, ref sKey, ref sValue, ref info) == true)
            {
                Fun_DisplayMessage_Qualified(info);

                //----------------------------------打开数据库连接----------------------------------                

                dbLib = new MyLibary.DataBaseLibary(new MyLibary.SqlLibary(), ref msg);

                dbLib.sqllib.SetSqlConnection(sValue);

                //配置文件 - 读取Sql服务器
                if (dbLib.sqllib.SqlConnection() == true)
                {
                    Fun_DisplayMessage_Qualified("服务器连接成功！");

                    //配置文件 - 读取Sql表名
                    if (cfgLib.GetConfigContent("appSettings", _tab, ref sKey, ref sValue, ref info) == true)
                    {
                        Fun_DisplayMessage_Qualified(info);

                        dbLib.sqllib.SetSqlQueryConditions("SELECT TOP 1 * FROM " + sValue);

                        dbLib.sqllib.SetSqlTable(sValue);

                        //SQL - 读取表字段
                        if (dbLib.sqllib.GetFieldName(ref fieldName) == true)
                        {
                            foreach (string s in fieldName)
                            {
                                cobReplaceFields.Items.Add(s);
                            }
                            cobReplaceFields.SelectedIndex = 0;

                            Fun_DisplayMessage_Qualified("表字段读取成功！");

                            //配置文件 - 读取工序名称
                            if (cfgLib.GetConfigContent("appSettings", "WorkProcess", ref sKey, ref sValue, ref info) == true)
                            {
                                string[] workprocess = sValue.Split(';');

                                foreach (string s in workprocess)
                                {
                                    cobWorkProcess.Items.Add(s);
                                }

                                cobWorkProcess.SelectedIndex = 0;

                                Fun_DisplayMessage_Qualified(info);
                            }
                            else
                            {
                                Fun_DisplayMessage_inQualified(info);
                            }
                        }
                        else
                        {
                            Fun_DisplayMessage_inQualified("表字段读取失败！");
                        }
                    }
                    else
                    {
                        Fun_DisplayMessage_inQualified(info);
                    }
                }
                else
                {
                    Fun_DisplayMessage_inQualified("服务器连接失败！");
                }
            }
            else
            {
                Fun_DisplayMessage_inQualified(info);
            }
        }
        public void Fun_DisplayMessage_Qualified(string msg)
        {
            rtbSqlStatement.SelectionColor = Color.Blue;
            rtbSqlStatement.SelectedText += msg + Environment.NewLine;
        }
        public void Fun_DisplayMessage_inQualified(string msg)
        {
            rtbSqlStatement.SelectionColor = Color.Red;
            rtbSqlStatement.SelectedText += msg + Environment.NewLine;
        }
        private void cobServiceDescription_MouseDown(object sender, MouseEventArgs e)
        {
            //2、定义txtGUID的显示信息
            ttTip.SetToolTip(cobServiceDescription, "连接服务器,点旁边按钮-> 。");
        }
        private void cobReplaceFields_MouseDown(object sender, MouseEventArgs e)
        {
            //2、定义txtGUID的显示信息
            ttTip.SetToolTip(cobReplaceFields, "选择要替换的字段，这里有中英文对照 。" + '\r' +
                            cobReplaceFields.Items[0].ToString() + " --- " + "记录ID号" + '\r' +
                            cobReplaceFields.Items[1].ToString() + " --- " + "产品序列号" + '\r' +
                            cobReplaceFields.Items[2].ToString() + " --- " + "工序名称" + '\r' +
                            cobReplaceFields.Items[3].ToString() + " --- " + "产品对应号" + '\r' +
                            cobReplaceFields.Items[4].ToString() + " --- " + "产品代码" + '\r' +
                            cobReplaceFields.Items[5].ToString() + " --- " + "产品版本" + '\r' +
                            cobReplaceFields.Items[6].ToString() + " --- " + "测试员工号" + '\r' +
                            cobReplaceFields.Items[7].ToString() + " --- " + "测试结果" + '\r' +
                            cobReplaceFields.Items[8].ToString() + " --- " + "描述内容" + '\r' +
                            cobReplaceFields.Items[9].ToString() + " --- " + "测试数据" + '\r' +
                            cobReplaceFields.Items[10].ToString() + " --- " + "测试的起始日期时间" + '\r' +
                            cobReplaceFields.Items[11].ToString() + " --- " + "测试的结束日期时间" + '\r' +
                            cobReplaceFields.Items[12].ToString() + " --- " + "检验状态" + '\r');
        }
        private void txtSourceContent_MouseDown(object sender, MouseEventArgs e)
        {
            //2、定义txtGUID的显示信息
            ttTip.SetToolTip(txtSourceContent, "替换之前的对象。");
        }
        private void txtRelacedContent_MouseDown(object sender, MouseEventArgs e)
        {
            //2、定义txtGUID的显示信息
            ttTip.SetToolTip(txtRelacedContent, "替换之后的对象。");
        }
        private void cobWorkProcess_MouseDown(object sender, MouseEventArgs e)
        {
            //2、定义txtGUID的显示信息
            ttTip.SetToolTip(cobWorkProcess, "选择工序名称。");
        }
        private void txtReplaceContent_MouseDown(object sender, MouseEventArgs e)
        {
            //2、定义txtGUID的显示信息
            ttTip.SetToolTip(txtReplaceContent, "记录着需要替换的内容。->点旁边的按钮");
        }


        #endregion

        #region ---------------------------------------------- Page2 File Equal ----------------------------------------------
        
        //--------------------Event--------------------------
        private void btnSourceFile_Click(object sender, EventArgs e)
        {
            fName = "";
            msg = "";

            if (Fun_FileDialog(ref fName, ref msg) == true)
            {
                txtSourceFilePath.Text = fName;
                Fun_DisplayMessage(btnSourceFile.Text + "路劲:" + fName);
            }
            else
            {
                MessageBox.Show(msg.ToString());
            }
        }
        private void btnEqualsFile_Click(object sender, EventArgs e)
        {
            fName = "";
            msg = "";
            fPath = "";

            if (Fun_FolderBrowserDialog(ref fName, ref fPath, out msg) == true)
            {
                txtEqualsFilePath.Text = fPath;//fName;
                Fun_DisplayMessage(btnTragetFile.Text + "路劲:" + fPath);//fName);                
            }
            else
            {
                MessageBox.Show(msg.ToString());
            }
        }
        private void btnEquals_Click(object sender, EventArgs e)
        {
            //Equals_FToF();
            Equals_FToD();            
        }

        //--------------------Math--------------------------

        public void Equals_FToF() 
        {
            //------------------读取原始数据与目标数据-------------------------------
            ds = excelLib.ExcelToDS(txtSourceFilePath.Text);

            string[,] s1 = new string[ds.Tables[0].Rows.Count, ds.Tables[0].Columns.Count];

            for (int i = 0; i <= ds.Tables[0].Rows.Count - 1; i++)
            {
                for (int j = 0; j <= ds.Tables[0].Columns.Count - 1; j++)
                {
                    s1[i, j] = ds.Tables[0].Rows[i][j].ToString();
                }
            }

            ds = excelLib.ExcelToDS(txtEqualsFilePath.Text);

            string[,] s2 = new string[ds.Tables[0].Rows.Count, ds.Tables[0].Columns.Count];

            for (int i = 0; i <= ds.Tables[0].Rows.Count - 1; i++)
            {
                for (int j = 0; j <= ds.Tables[0].Columns.Count - 1; j++)
                {
                    s2[i, j] = ds.Tables[0].Rows[i][j].ToString();
                }
            }


            //------------------比对原始数据与目标数据-------------------------------
            StringBuilder sb1 = new StringBuilder();
            StringBuilder sb2 = new StringBuilder();
            int i1 = 0;
            int i2 = 0;
            for (int i = 0; i <= ds.Tables[0].Rows.Count - 1; i++)
            {
                for (int j = 0; j <= ds.Tables[0].Columns.Count - 1; j++)
                {
                    if (s1[i, j].Equals(s2[i, j]))
                    {
                        i1++;

                        if (i1 == 1)
                        {
                            sb1.Append("-------------------------------------------------------" + Environment.NewLine);
                            sb1.Append("                            same                       " + Environment.NewLine);
                            sb1.Append("-------------------------------------------------------" + Environment.NewLine);
                        }
                        else
                        {
                            sb1.Append(s1[i, j] + "-" + s2[i, j] + "-same" + Environment.NewLine);
                        }
                    }
                    else
                    {
                        i2++;

                        if (i2 == 1)
                        {
                            sb2.Append("-------------------------------------------------------" + Environment.NewLine);
                            sb2.Append("                            diff                       " + Environment.NewLine);
                            sb2.Append("-------------------------------------------------------" + Environment.NewLine);
                        }
                        else
                        {
                            sb2.Append(s1[i, j] + "-" + s2[i, j] + "-different" + Environment.NewLine);
                        }
                    }
                }
            }

            fileLib.FS_WriteFile(System.IO.Directory.GetCurrentDirectory() + @"\diary.txt",
                sb1.ToString(), ref msg);
            fileLib.FS_WriteFile(System.IO.Directory.GetCurrentDirectory() + @"\diary.txt",
                sb2.ToString(), ref msg);
            Fun_DisplayMessage("数据比对完成！详细内容见：" + System.IO.Directory.GetCurrentDirectory() + @"\diary.txt");
        }
        public void Equals_FToD()
        {
            //------------------读取原始数据与目标数据-------------------------------
            ds = excelLib.ExcelToDS(txtSourceFilePath.Text,1);

            int _columnCount = ds.Tables[0].Columns.Count;

            int _crcColumnCount = 0;

            if (_columnCount == 0 )
            {
                MessageBox.Show("指定的原始数据文件中没有数据，请查看后继续！","错误提示"
                    ,MessageBoxButtons.OK,MessageBoxIcon.Error);
                return;
            }
            else
            {
                _crcColumnCount = 1;
            }

            string[,] s1 = new string[ds.Tables[0].Rows.Count, ds.Tables[0].Columns.Count];
          
            for (int i = 0; i <= ds.Tables[0].Rows.Count - 1; i++)
            {
                for (int j = 0; j <= _crcColumnCount - 1; j++)//ds.Tables[0].Columns.Count - 1
                {
                    s1[i, j] = ds.Tables[0].Rows[i][j].ToString();
                }
            }

            //C#遍历指定文件夹中的所有文件 
            DirectoryInfo TheFolder = new DirectoryInfo(txtEqualsFilePath.Text);
            string[,] s2 = new string[TheFolder.GetFiles().Length,2];
            string[] s3 = null;
            string[] ProcessArray = null;
            int ic2 = -1;
            int NodeCount = 0;            
            int SearchDeep = 10;

            //遍历文件
            foreach (FileInfo NextFile in TheFolder.GetFiles())
            {                
                ic2++;
                Fun_DisplayEqualsDetail("一共有: " + TheFolder.GetFiles().Length +
                                         " / 已经完成: " + (ic2 + 1).ToString());                
                s3 = NextFile.Name.Split('_');
                s2[ic2,0] = s3[0];

                xmlstreamrwable.OpentXML(NextFile.FullName, ref msg);
                xmlstreamrwable.GetXMLContent("TestResult", "SerialNumber", ref NodeCount,
                    ref ProcessArray, SearchDeep, ref msg);
                xmlstreamrwable.CloseXmlReader();
                s2[ic2, 1] = ProcessArray[0];
            }

            //------------------比对原始数据与目标数据-------------------------------
            //拿excel中从row：1，column：1~有效数据范围的数据放入A
            //拿A与文件夹B中文件比较（文件名包含09码，21码），
            //如果找到   - find
            //如果没找到 - nofind
            StringBuilder sb1 = new StringBuilder();
            StringBuilder sb2 = new StringBuilder();
            int i1 = -1;
            int i2 = -1;
            bool bfind = false;

            for (int i = 0; i <= ds.Tables[0].Rows.Count - 1; i++)
            {
                if (bfind == true) { bfind = false; }

                for (int j = 0; j <= _crcColumnCount - 1; j++)//ds.Tables[0].Columns.Count - 1
                {
                    if (bfind == true) { break; }

                    for (int k = 0; k <= s2.GetUpperBound(0); k++)
                    {
                        if (s1[i, j].Trim().Equals(s2[k, 0].Trim()) || s1[i, j].Trim().Equals(s2[k, 1].Trim()))
                        {
                            i1++;

                            if (i1 == 0)
                            {
                                sb1.Append("-------------------------------------------------------" + Environment.NewLine);
                                sb1.Append("                            find                       " + Environment.NewLine);
                                sb1.Append("-------------------------------------------------------" + Environment.NewLine);
                                bfind = true;
                                sb1.Append(s1[i, j] + "-" + s2[k, 0] + Environment.NewLine);
                                break;
                            }
                            else
                            {
                                bfind = true;
                                sb1.Append(s1[i, j] + "-" + s2[k, 0] + Environment.NewLine);
                                break;
                            }
                        }
                        else
                        {
                            i2++;

                            if (i2 == 0)
                            {
                                sb2.Append("-------------------------------------------------------" + Environment.NewLine);
                                sb2.Append("                           nofind                      " + Environment.NewLine);
                                sb2.Append("-------------------------------------------------------" + Environment.NewLine);
                            }

                            if (k == s2.GetUpperBound(0))
                            {
                                sb2.Append(s1[i, j] + Environment.NewLine);
                            }                            
                        }
                    }
                
                }
            }

            sb1.Append(sb2.ToString());

            if (File.Exists(System.IO.Directory.GetCurrentDirectory() + @"\diary.txt") == true)
            {
                File.Delete(System.IO.Directory.GetCurrentDirectory() + @"\diary.txt");
            }

            fileLib.FS_WriteFile(System.IO.Directory.GetCurrentDirectory() + @"\diary.txt",
                sb1.ToString(), ref msg);

            Fun_DisplayMessage("数据比对完成！详细内容见：" + 
                System.IO.Directory.GetCurrentDirectory() + @"\diary.txt");

        }

        public void Fun_DisplayEqualsDetail(string msg)
        {
            this.Text = msg;
        }
        public void Fun_DisplayMessage(string msg) 
        {
            rtb_Message.Text += msg + Environment.NewLine;
        }
        public bool Fun_FileDialog(ref string fName, ref string msg)
        {
            try
            {
                OpenFileDialog openFileDialog = new OpenFileDialog();
                openFileDialog.InitialDirectory = System.IO.Directory.GetCurrentDirectory();
                openFileDialog.Filter = "所有文件|*.*|Excel文件|*.xls|XML文件|*.xml|文本文件|*.txt";
                openFileDialog.RestoreDirectory = true;
                openFileDialog.FilterIndex = 1;

                switch (openFileDialog.ShowDialog())
                {
                    case DialogResult.OK:
                        fName = openFileDialog.FileName;
                        msg = fName;
                        break;

                    case DialogResult.Cancel:
                        fName = "";
                        msg = "";
                        break;
                }
                return true;
            }
            catch (Exception exc)
            {
                msg = exc.ToString();
                return false;
            }

        }
        public bool Fun_FolderBrowserDialog(ref string folderName,
                                            ref string folderPath,
                                            out string msg)
        {
            try
            {
                if (fdb_SourceFile.ShowDialog() == DialogResult.OK)
                {
                    folderPath = fdb_SourceFile.SelectedPath;

                    string[] s1 = fdb_SourceFile.SelectedPath.Split('\\');

                    folderName = "\\" + s1[s1.GetUpperBound(0)] + "\\";
                }
                else
                {
                    msg = "";
                    return false;
                }
                msg = "";
                return true;
            }
            catch (Exception exc)
            {
                msg = exc.ToString();
                return false;
            }
        }

        #endregion

        #region ---------------------------------------------- Page3 Filter ----------------------------------------------

        //--------------------------------------Event-------------------------------------------
        private void btnFilter_LoadSourceFile_Click(object sender, EventArgs e)
        {
            fName = "";
            msg = "";

            ButtonBase bb = (ButtonBase)sender;
            
            if (Fun_FileDialog(ref fName, ref msg) == true)
            {
                txtFilterSourceFilePath.Text = fName;
                Fun_DisplayMessage(rtb_FilterMessage, bb.Text + "路劲:" + Environment.NewLine + fName);
            }
            else
            {
                MessageBox.Show(msg.ToString());
            }
        }

        private void btnFilter_DoFilter_Click(object sender, EventArgs e)
        {
            DoFilter(txtFilterSourceFilePath.Text);
        }


        //--------------------------------------Math-------------------------------------------
        
        /// <summary>
        /// 过滤指定文件路径的数据
        /// </summary>
        private void DoFilter(string filePath)
        {
            string[,] strArrayObj = DoDSToArray(excelLib.ExcelToDS(filePath));

            DoQueryProductsAllProcess(strArrayObj);
        }
        /// <summary>
        /// 将指定路劲的Excel对象中的内容转化为DataSet对象
        /// </summary>
        /// <param name="filePath">Excel对象的路径</param>
        /// <returns></returns>
        private DataSet DoExcelToDS(string filePath)
        {
            //------------------读取原始数据与目标数据-------------------------------
            try
            {
                return excelLib.ExcelToDS(filePath);
            }
            catch (System.Exception ex)
            {
            	MessageBox.Show(ex.Message);

                return null;
            }

        }
        /// <summary>
        /// 将DataSet对象转化为数组对象
        /// </summary>
        /// <param name="ds">DataSet对象</param>
        /// <returns>数组对象</returns>
        private string[,] DoDSToArray(DataSet ds)
        {
            try
            {
                if (ds.Tables[0].Columns.Count == 0)
                {
                    MessageBox.Show("指定的原始数据文件中没有数据，请查看后继续！", "错误提示"
                        , MessageBoxButtons.OK, MessageBoxIcon.Error);

                    return null;
                }
                else
                {
                    string[,] _strArrayObj = new string[ds.Tables[0].Rows.Count, ds.Tables[0].Columns.Count];

                    for (int i = 0; i <= ds.Tables[0].Rows.Count - 1; i++)
                    {
                        for (int j = 0; j <= ds.Tables[0].Columns.Count - 1; j++)
                        {
                            _strArrayObj[i, j] = ds.Tables[0].Rows[i][j].ToString();
                        }
                    }

                    return _strArrayObj;
                }
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message);

                return null;
            }

        }
        /// <summary>
        /// 在指定的richtextbox中显示数据
        /// </summary>
        /// <param name="rtb">指定的richtextbox</param>
        /// <param name="msg">数据</param>
        private void Fun_DisplayMessage(RichTextBox rtb, string msg)
        {
            rtb.Text += msg + Environment.NewLine;
        }
        public void Fun_DisplayMessage_Qualified(RichTextBox rtb, string msg)
        {
            rtb.SelectionColor = Color.Blue;
            rtb.SelectedText += msg + Environment.NewLine;
        }
        public void Fun_DisplayMessage_inQualified(RichTextBox rtb, string msg)
        {
            rtb.SelectionColor = Color.Red;
            rtb.SelectedText += msg + Environment.NewLine;
        }


        private void DoQueryProductsAllProcess(string[,] ProductID)
        {
            string _configValue = "";
            string _configKey = "";
            string _serviceDescription = "BigShop_SqlConnectionString";
            string _serviceDescription2 = "WorkProcessForFilter";
            string _sqlConnectionString = "";
            string _processContent;

            if (DoGetConfigContent(_serviceDescription, ref _configValue, ref _configKey) == true)
            {
                _sqlConnectionString = _configValue;

                if (DoConnectiongSQL(_sqlConnectionString) == true)
                {
                    if (DoGetConfigContent(_serviceDescription2, ref _configValue, ref _configKey) == true)
                    {
                        _processContent = _configValue;

                        DoQueryProductsNameResultTestingVersion(ProductID, _processContent);
                    }
                    else
                    {
                        return;
                    }
                } 
                else
                {
                    return;
                }
            } 
            else
            {
                return;
            }

            DoCloseSQL();
        }
        private bool DoGetConfigContent(string ServiceDescription, ref string configValue,
            ref string configKey)
        {
            try
            {
                string _value = "";
                string _key = "";

                if (cfgLib.GetConfigContent("appSettings", ServiceDescription, ref _key, 
                    ref _value, ref info) == true)
                {
                    configValue = _value;
                    configKey = _key;
                    Fun_DisplayMessage_Qualified(rtb_FilterMessage, "配置文件读取成功！");
                    return true;
                }
                else
                {
                    Fun_DisplayMessage_inQualified(rtb_FilterMessage, "配置文件读取失败！");
                    return false;
                }

            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message);

                return false;
            }
        }
        private bool DoConnectiongSQL(string sqlConnectionString)
        {
            try
            {
                //----------------------------------打开数据库连接----------------------------------                
                dbLib = new MyLibary.DataBaseLibary(new MyLibary.SqlLibary(), ref msg);

                dbLib.sqllib.SetSqlConnection(sqlConnectionString);

                //配置文件 - 读取Sql服务器
                if (dbLib.sqllib.SqlConnection() == true)
                {
                    Fun_DisplayMessage_Qualified(rtb_FilterMessage, "服务器连接成功！");
                    return true;
                }
                else
                {
                    Fun_DisplayMessage_inQualified(rtb_FilterMessage, "服务器连接失败！");
                    return false;
                }
                
            }
            catch (System.Exception ex)
            {
                Fun_DisplayMessage_inQualified(rtb_FilterMessage, 
                    "服务器连接失败！" + Environment.NewLine + "详细内容：" + ex.Message);
                return false;
            }

        }
        private void DoQueryProductsNameResultTestingVersion(string[,] ProcessID, string ProcessContent)
        {
            string[] _workprocess = ProcessContent.Split(';');
            string _queryConditions = "";
            string[,] _FilterContents = new string[ProcessID.Length, _workprocess.Length];           
            char _segmentationSigle = ',';
            string[] _queryFields = cfgLib.ConfigContent("appSettings", "QueryFieldsForFilter").Split(_segmentationSigle);
            StringBuilder _sb = new StringBuilder();
            SqlDataReader _sqlreader = null; 

            for (int i = 0; i <= ProcessID.GetUpperBound(0); i++  )
            {
                for (int j = 0; j <= _workprocess.GetUpperBound(0); j++ )
                {
                    _queryConditions = "SELECT " + cfgLib.ConfigContent("appSettings", "QueryFieldsForFilter") + " " +
                        "FROM " + cfgLib.ConfigContent("appSettings", "BigShop_Table") + " " +
                        "WHERE ID IN " +
                        "(SELECT MAX(ID) " +
                        "FROM " + cfgLib.ConfigContent("appSettings", "BigShop_Table") + " " +
                        "WHERE ProductID='" + ProcessID[i, 0] + "' AND " +
                        "Result='0'  AND Process = '" + _workprocess[j] + "')";
                    
                    dbLib.sqllib.SetSqlQueryConditions(_queryConditions);

                    _sqlreader = dbLib.sqllib.Query_SqlDataReader(_queryConditions);

                    while (_sqlreader.Read())
                    {
                        for (int k = 0; k <= _queryFields.GetUpperBound(0); k++)
                        {
                            if (k < _queryFields.GetUpperBound(0))
                            {
                                _sb.Append(_sqlreader.GetString(k) + _segmentationSigle);
                            } 
                            else
                            {
                                _sb.Append(_sqlreader.GetString(k));
                            }                            
                        }                                               
                    }

                    _FilterContents[i, j] = _sb.ToString();

                    _sb.Remove(0, _sb.Length);

                    _sqlreader.Close();
                }
            }
        }

        private void DoCloseSQL()
        {
            try
            {
                dbLib.sqllib.CloseConnection();
                Fun_DisplayMessage_Qualified(rtb_FilterMessage, "数据库关闭成功！");
            }
            catch (System.Exception ex)
            {
                Fun_DisplayMessage_inQualified(rtb_FilterMessage, "数据库关闭失败！详细内容：" + ex.Message);
            }            
        }

        #endregion



    }
}
